<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>链表结构2-继续扩展</h1>
<script>
    // 数据结构
    class Node {
      constructor(element){
          this.element = element
          this.next = null
      }
  }
  // 关联数据结构
  class linkNode{
        constructor() {
            this.count = 0
            this.header = null
        }
        push(element){ // 添加数据
            let node = new Node(element)
            console.log(node)
            if(this.header == null){
                this.header = node
            }else {
                let current = this.header
                while (current.next != null){
                    current = current.next
                }
                current.next = node
            }
            this.count ++
        }
      // 指定位置删除
      removeAt(index){
          // 必须大于等于 0 并且小于我们的长度吧
          if(index>=0 && index<this.count){
              let current = this.header;// 当前节点
              if(index==0){
                  this.header = this.header.next
              }else{
                  let previous;// 上一个节点
                  for(let i = 0 ;i<index;i++){
                      previous = current
                      current = current.next
                  }
                  previous.next = current.next
              }
              this.count--
              return current
          }
          return
      }

        // 指定位置删除2
      removeAt2(index){
            // 必须大于等于 0 并且小于我们的长度吧
          if(index>=0 && index<this.count){
              let current = this.header
              if(index==0){
                  this.header = this.header.next
              }else{
                  let previous  = this.getNodeAt(index-1)
                  current = previous.next
                  previous.next = current.next
              }
              this.count --
              return current.element
          }
          return
      }

      // 获取某个位置的节点
      getNodeAt(index){
          if(index>=0 && index<this.count){
              let node = this.header;// 当前节点
              for(let i = 0 ;i<index;i++){
                  node = node.next
              }
              return node
          }
          return
      }

      equalFn(a,b){ // 防止传出的数据 是引用类型的
        return JSON.stringify(a) === JSON.stringify(b)
      }

      // 返回索引值
      indexOf(element){
          let node = this.header;// 当前节点
          for(let i = 0 ;i<this.count;i++) {
              console.log(node.element,element,this.equalFn(node.element, element))
              if (this.equalFn(node.element, element)) {
                  return i
              }
              node = node.next
          }
          return -1
      }

      // 根据值删除
      remove(element){
          let index = this.indexOf(element)
          return this.removeAt2(index)
      }
      // 任意位置 插入
      insert(element,index){
            if(index>=0 && index<=this.count){
                const node = new Node(element)
                if(index==0){ // 链头
                    node.next = this.header
                    this.header = node
                }else{
                    const previos = this.getNodeAt(index-1)
                    node.next = previos.next
                    previos.next = node

                }
            this.count++
            }
            return
      }
      // 链表的大小
      size(){
            return this.count
      }
      // 链表是不是空
      isEmpty(){
            return this.size() === 0
      }
      // 返回列头
      getHead(){
            return this.header
      }
      // 清空链表
      clear(){
            this.count = 0
            this.header = null
      }
  }
  let list = new linkNode()
    list.push(100)
    list.push(200)
     list.push(300)
    list.insert({a:1},2)
    console.log(list)
</script>
</body>
</html>
